import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @License: (C) Copyright 2013-2022
 * @Contact: abcque@outlook.com
 * @IDE: IntelliJ IDEA
 * @Encoding: utf-8
 * @Project: st2214st2215
 * @Package: PACKAGE_NAME
 * @File: ClassInstance.java
 * @Time: 2023/5/30 16:12
 * @Author: abcque
 * @Description:
 * @Modifier:
 * @ModifyDate:
 * @ModifyRemark:
 */
public class ClassInstance {
    public static void main(String[] args) {
        try {
            Class<?> personClass = Class.forName("Person");

            // 通过调用无参构造器创建对象
            Object object1 = personClass.newInstance();

            System.out.println(object1);

            Constructor<?> declaredConstructor = personClass.getDeclaredConstructor(String.class, int.class);
            Object object2 = declaredConstructor.newInstance("abc", 18);
            System.out.println(object2);

        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
